<template>
  <div>
    <div class="center">
      <h1>欢迎参加秒杀活动</h1>
      <img v-bind:src="goodsImg" width="250px" id="img1" />
      <div v-html="goodsName"></div>
      <br />
      <div v-html="goodsDisc"></div>
      <br />
      <div v-html="goodsPrice" id="times"></div>
      <br />
      <div v-html="goodsStock"></div>
      <br />
      <div v-html="times" id="times"></div>
      <br />

      <el-button type="danger" round @click="doSeckill">点击开抢！</el-button
      ><br />
      <br />

      <el-button type="primary" round @click="searchSecOrders"
        >查询历史秒杀订单</el-button
      >
    </div>
  </div>
</template>

<script>
import router from "../../router";
var goodsNameByOpen = "";
export default {
  data() {
    return {
      goodsImg: "",
      goodsName: "",
      goodsDisc: "",
      goodsStock: "",
      goodsPrice: "",
      count: "",
      times: "",
    };
  },

  beforeCreate() {},

  //钩子函数，用于查看是否有秒杀活动,并且实时更新库存
  mounted() {
    //获取参与秒杀的商品名称
    this.getSeckillGoods();

    //实时更新库存
    setInterval(() => {
      this.flushstock();
    }, 1000);
  },
  methods: {
    //查询历史秒杀订单
    searchSecOrders() {
      router.push("./seckillOrders");
    },

    //获取参与秒杀的商品名称
    getSeckillGoods() {
      this.$axios
        .get("/seckill/getGoodsByIsSeckill", {
          headers: {
            "x-access-token": localStorage.getItem("x-access-token"),
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            goodsNameByOpen = res.data.extend.result.goodsName;

            this.confirmSeckill();
          } else {
            if (
              res.data.code == 400 &&
              res.data.extend.result == "暂无该商品"
            ) {
              this.$message.error("现在暂无秒杀活动");
              router.push("/");
            }
          }
        });
    },

    //用于查看是否有秒杀活动
    confirmSeckill() {
      this.$axios
        .get("/seckill/getSeckillGoods/" + goodsNameByOpen, {
          headers: {
            "x-access-token": localStorage.getItem("x-access-token"),
          },
        })
        .then((res) => {
          if (
            res.data.code == 400 &&
            res.data.extend.result == "秒杀活动已结束"
          ) {
            this.$message.error("秒杀活动已结束");
            router.push("/");
          }
          if (res.data.code == 400 && res.data.extend.result == "暂无该商品") {
            this.$message.error("现在暂无秒杀活动");
            router.push("/");
          }
          if (
            res.data.code == 400 &&
            res.data.extend.result == "秒杀活动还未开始"
          ) {
            this.$message.error("秒杀活动还未开始");
            router.push("/");
          }
          if (
            res.data.code == 400 &&
            res.data.extend.result == "该商品不是秒杀活动商品"
          ) {
            this.$message.error("现在暂无秒杀活动");
            router.push("/");
          }
          if (res.data.code == 200) {
            this.$message({
              message: "欢迎参加秒杀活动",
              type: "success",
            });

             console.log(res.data.extend.goods.goodsPic)
            if (null != res.data.extend.goods.goodsPic) {
              this.goodsImg =
                res.data.extend.goods.goodsPic ;
            } else {
              this.goodsImg = "";
            }

            this.goodsName = res.data.extend.goods.goodsName;
            this.goodsDisc = res.data.extend.goods.goodsDisc;
            this.goodsPrice =
              "秒杀价仅需：" + res.data.extend.goods.goodsPrice + " 元";
          }
        });
    },
    //更新库存和时间
    flushstock() {
      this.$axios
        //暂时写死商品，因为后台秒杀管理还未完成
        .get("/seckill/getSeckillStock/" + goodsNameByOpen, {
          headers: {
            "x-access-token": localStorage.getItem("x-access-token"),
          },
        })
        .then((res) => {
          this.goodsStock = "剩余库存数:" + res.data.extend.stock;
          var a = new Date(res.data.extend.endTime).getTime();
          this.times = "距离秒杀结束还剩:" + this.countDownSec(a);
        });
    },
    //秒杀按钮
    doSeckill() {
      this.$axios
        //暂时写死商品，因为后台秒杀管理还未完成
        .get("/seckill/doSeckill/" + goodsNameByOpen, {
          headers: {
            "x-access-token": localStorage.getItem("x-access-token"),
          },
        })
        .then((res) => {
          if (
            res.data.code == 400 &&
            res.data.extend.result == "用户已参加过"
          ) {
            this.$message.error(
              "您已经参加过本次秒杀活动，请下次再来，即将为您跳转到秒杀订单查询页面"
            );
            //倒计时三秒跳转到秒杀订单查询页面
            this.countDown1();
          }
          if (res.data.code == 400 && res.data.extend.result == "商品已售空") {
            this.$message.error(
              "已经抢光啦，下次手速快一点哦，即将为您跳转到商城首页"
            );
            //倒计时三秒跳转到商城首页
            this.countDown();
          }

          if (res.data.code == 200 && res.data.extend.result == "秒杀成功") {
            this.$message({
              message:
                "恭喜您秒杀成功，即将为您创建订单并跳转到秒杀订单查询页面",
              type: "success",
            });
            //倒计时三秒跳转到秒杀订单查询页面
            this.countDown1();
          }
          if (res.data.code == 400 && res.data.extend.result == "秒杀失败") {
            this.$message.error("秒杀失败，请再次点击开抢吧！");
          }
        });
    },
    //倒计时三秒跳转到商城首页
    countDown() {
      const TIME_COUNT = 2;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.timer = setInterval(() => {
          if (this.count > 1 && this.count <= TIME_COUNT) {
            //限制倒计时区间
            this.count--;
          } else {
            clearInterval(this.timer); //删除定时器
            this.timer = null;
            //跳转的页面写在此处
            router.push("/");
          }
        }, 1000);
      }
    },

    //倒计时三秒跳转到订单查询
    countDown1() {
      const TIME_COUNT = 2;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.timer = setInterval(() => {
          if (this.count > 1 && this.count <= TIME_COUNT) {
            //限制倒计时区间
            this.count--;
          } else {
            clearInterval(this.timer); //删除定时器
            this.timer = null;
            //跳转的页面写在此处
            router.push("./seckillOrders");
          }
        }, 1000);
      }
    },

    //秒杀时间倒计时
    countDownSec(inputTime) {
      //获取从1970年1月1日到现在的时间的毫秒数
      var nowTime = +new Date();
      //获取输入时间的毫秒数
      var putTime = +new Date(inputTime);
      //剩余时间总的毫秒数 除以 1000 变为总秒数
      var dec = (putTime - nowTime) / 1000;
      //得到天 格式化成前缀加零的样式
      var d = parseInt(dec / 60 / 60 / 24);
      d = d < 10 ? "0" + d : d;
      //得到小时 格式化成前缀加零的样式
      var h = parseInt((dec / 60 / 60) % 24);
      h = h < 10 ? "0" + h : h;
      //得到分钟 格式化成前缀加零的样式
      var m = parseInt((dec / 60) % 60);
      m = m < 10 ? "0" + m : m;

      //得到秒 格式化成前缀加零的样式
      var s = parseInt(dec % 60);
      s = s < 10 ? "0" + s : s;

      return `${d}天${h}时${m}分${s}秒`;
    },
  },
};
</script>

<style>
.center {
  text-align: center;
}
#times {
  color: red;
}
</style>